<template>
  <div class="modal" v-if="show">
    <div class="box" ref="box">
      内容
      <button>hello</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useClickOutside } from "../hooks/useClickOutside";

defineProps(["show"]);
const emit = defineEmits(["close"]);

const box = ref(null);

useClickOutside(box, (isClickOutside) => {
  if (isClickOutside) {
    // alert("点击了外层白色");
    emit("close");
  } else {
    // alert("点击了 box 元素自身");
  }
});
</script>

<style>
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 300px;
  height: 300px;
  background-color: #fff;
}
</style>
